<?php
class Apply extends BZ_Controller
{
    public function _init()
    {
        $this->load->helper(array('common', 'validation'));
        $this->load->model('apply_model', 'apply');
        $this->load->model('region_model', 'region');
        $this->load->model('tourism_provider_model','tourism_provider');
        $this->load->model('user_model','user');
    }

    /**
     * 我要供应
     */
    public function applyProvider()
    {
        if ( !empty($this->uid)) {
            $result = $this->tourism_provider->getProvider(array('uid' => $this->uid, 'limit' => 1));
            if ($result->num_rows() > 0) {
                $this->redirect(site_url('tourism/tourismprovider/grid'));
            }
        }
        $this->load->view('home/apply/applyProvider', $this->data);
    }

    /**
     * 我要分销
     */
    public function applySeller()
    {
        $this->load->view('home/apply/applySeller', $this->data);
    }

    /**
     * 我要开通抽奖
     */
    public function applyLottery()
    {
        $this->load->view('home/apply/applyLottery', $this->data);
    }

    /**
     * 我要开店
     */
    public function applyShop()
    {
        $this->load->view('home/apply/applyShop', $this->data);
    }

    /**
     * 我要推广
     */
    public function applySpread()
    {
        $this->load->view('home/apply/applySpread', $this->data);
    }

    private function _validate()
    {
        $postData = $this->input->post();
        if (empty($postData['contacts']) || strlen($postData['contacts']) < 2) {
            $this->jsonMessage('请填联系人姓名');
        }

        if (!validatePhone($postData['phone'])) {
            $this->jsonMessage('请填写正确的联系方式');
        }

        $regionIds = array($postData['provinceid'], $postData['cityid'], $postData['areaid']);
        $result = $this->region->findRegionIds($regionIds);
        if ($result->num_rows() != 3) {
            $this->jsonMessage('请选择所在区域');
        }
        switch ($postData['category']) {
            case 1:
                if (empty($postData['company'])) {
                    $this->jsonMessage('请填写正确的公司名称');
                }
                if (empty($postData['product'])) {
                    $this->jsonMessage('请填写产品类型');
                }
                $postData['details']['product'] = $postData['product'];
                break;
            case 2:
                if ( !empty($postData['sfz']) && !checkSfz($postData['sfz'])) {
                    $this->jsonMessage('请输入正确的身份证');
                }
                $postData['details']['sfz'] = $postData['sfz'];
                $postData['details']['business'] = $postData['business'];
                break;
            case 3:
                if (empty($postData['company'])) {
                    $this->jsonMessage('请填写正确的公司名称');
                }
                if (empty($postData['business'])) {
                    $this->jsonMessage('请填写经营范围');
                }
                $postData['details']['business'] = $postData['business'];
                break;
            case 4:
                $postData['details']['business'] = $postData['business'];
                break;
            case 5:
                $postData['details']['product'] = $postData['product'];
                break;
            default:
                break;
        }
        if ( !empty($postData['user_name'])) {
            if ( !$this->checkUsername($postData['user_name'])) {
                $this->jsonMessage('请填写正确的推荐人');
            }
        }
        $postData['details']['company'] = $postData['company'];
        return $postData;
    }

    /**
     * 申请提交
     */
    public function applySubmit()
    {
        $postData = $this->_validate();
        $insertId = $this->apply->insertApply($postData);
        if ($insertId) {
            $this->jsonMessage('');
        } else {
            $this->jsonMessage('请选择所在区域');
        }
    }

    /**
     * 验证用户名是否存在
     * @param $userName
     * @return bool
     */
    private function checkUsername($userName)
    {
        if ($userName) {
            $user = $this->user->validateName($userName);
            if ($user->num_rows() > 0) {
                return TRUE;
            }
        }
        return FALSE;
    }
}